package br.com.tiespec.ejb.Ejb;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;

import br.com.tiespec.ejb.Dao.CenarioBaseDAO;
import br.com.tiespec.ejb.Dao.FluxoBaseDAO;
import br.com.tiespec.ejb.EjbInterface.CenarioBaseInterface;
import br.com.tiespec.model.Enum.TipoSortOrder;
import br.com.tiespec.model.Modelo.CenarioBase;
import br.com.tiespec.model.Modelo.FluxoBase;
import br.com.tiespec.model.Util.DAOException;
import br.com.tiespec.model.Util.RNException;

@Stateless(mappedName = "CenarioBaseImpl")
@Local(value=CenarioBaseInterface.class)
public class CenarioBaseImpl implements CenarioBaseInterface  {

	private static final long serialVersionUID = 1L;
	
	@EJB CenarioBaseDAO cbDao;
	@EJB FluxoBaseDAO fbDao;
	
	@Override
	public void salvarCenario(CenarioBase cenarioBaseInstance)
			throws RNException {
		try {
			for (FluxoBase b:cenarioBaseInstance.getFluxosBase()){
				b.setCenarioBase(cenarioBaseInstance);
			}
			cbDao.save(cenarioBaseInstance);
		} catch (DAOException e) {
			throw new RNException(e);
		}
	}

	@Override
	public void atualizarCenario(CenarioBase cenarioBaseInstance)
			throws RNException {
		try {
			for (FluxoBase b:cenarioBaseInstance.getFluxosBase()){
				b.setCenarioBase(cenarioBaseInstance);
			}
			cbDao.update(cenarioBaseInstance);
		} catch (DAOException e) {
			throw new RNException(e);
		}
	}

	@Override
	public void excluirCenario(Long id) throws RNException {
		try {
			cbDao.remover(id);
		} catch (DAOException e) {
			throw new RNException(e);
		}
	}

	@Override
	public List<CenarioBase> listarCenarioBase(int first, int pageSize,
			String sortField, TipoSortOrder sortOrder,
			Long idClientePortal, String varNome, String varFluxo) {
		return cbDao.listarCenarioBase(first, pageSize, sortField, sortOrder, idClientePortal, varNome, varFluxo);
	}

	@Override
	public int countCenarioBase(Long idClientePortal, String varNome,
			String varFluxo) {
		return cbDao.countCenarioBase(idClientePortal, varNome, varFluxo);
	}

	@Override
	public CenarioBase selecionarCenarioBase(Long id) {
		return cbDao.selecionar(id);
	}
	
	@Override
	public CenarioBase selecionarCenarioBaseComResponsavel(Long id) {
		return cbDao.selecionarComResponsavel(id);
	}
	
	@Override
	public CenarioBase selecionarCenarioBaseComFluxos(Long id) {
		return cbDao.selecionarComFluxos(id);
	}
	
	public CenarioBase selecionarCenarioBaseComFluxosEResponsavel(Long id) {
		return cbDao.selecionarComFluxosEResponsavel(id);
	}
}